package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.FeedTaskQualityDAO;
import cn.edu.scau.cmi.hombio.dao.FodderTypeDAO;
import cn.edu.scau.cmi.hombio.dao.TaskPigstyDAO;

import cn.edu.scau.cmi.hombio.domain.FeedTaskQuality;
import cn.edu.scau.cmi.hombio.domain.FodderType;
import cn.edu.scau.cmi.hombio.domain.TaskPigsty;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for FeedTaskQuality entities
 * 
 */

@Service("FeedTaskQualityService")

@Transactional
public class FeedTaskQualityServiceImpl implements FeedTaskQualityService {

	/**
	 * DAO injected by Spring that manages FeedTaskQuality entities
	 * 
	 */
	@Autowired
	private FeedTaskQualityDAO feedTaskQualityDAO;

	/**
	 * DAO injected by Spring that manages FodderType entities
	 * 
	 */
	@Autowired
	private FodderTypeDAO fodderTypeDAO;

	/**
	 * DAO injected by Spring that manages TaskPigsty entities
	 * 
	 */
	@Autowired
	private TaskPigstyDAO taskPigstyDAO;

	/**
	 * Instantiates a new FeedTaskQualityServiceImpl.
	 *
	 */
	public FeedTaskQualityServiceImpl() {
	}

	/**
	 */
	@Transactional
	public FeedTaskQuality findFeedTaskQualityByPrimaryKey(Integer id) {
		return feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(id);
	}

	/**
	 * Save an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public FeedTaskQuality saveFeedTaskQualityTaskPigsty(Integer id, TaskPigsty related_taskpigsty) {
		FeedTaskQuality feedtaskquality = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(id, -1, -1);
		TaskPigsty existingtaskPigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(related_taskpigsty.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtaskPigsty != null) {
			existingtaskPigsty.setId(related_taskpigsty.getId());
			existingtaskPigsty.setMemo(related_taskpigsty.getMemo());
			related_taskpigsty = existingtaskPigsty;
		} else {
			related_taskpigsty = taskPigstyDAO.store(related_taskpigsty);
			taskPigstyDAO.flush();
		}

		feedtaskquality.setTaskPigsty(related_taskpigsty);
		related_taskpigsty.getFeedTaskQualities().add(feedtaskquality);
		feedtaskquality = feedTaskQualityDAO.store(feedtaskquality);
		feedTaskQualityDAO.flush();

		related_taskpigsty = taskPigstyDAO.store(related_taskpigsty);
		taskPigstyDAO.flush();

		return feedtaskquality;
	}

	/**
	 * Delete an existing TaskPigsty entity
	 * 
	 */
	@Transactional
	public FeedTaskQuality deleteFeedTaskQualityTaskPigsty(Integer feedtaskquality_id, Integer related_taskpigsty_id) {
		FeedTaskQuality feedtaskquality = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(feedtaskquality_id, -1, -1);
		TaskPigsty related_taskpigsty = taskPigstyDAO.findTaskPigstyByPrimaryKey(related_taskpigsty_id, -1, -1);

		feedtaskquality.setTaskPigsty(null);
		related_taskpigsty.getFeedTaskQualities().remove(feedtaskquality);
		feedtaskquality = feedTaskQualityDAO.store(feedtaskquality);
		feedTaskQualityDAO.flush();

		related_taskpigsty = taskPigstyDAO.store(related_taskpigsty);
		taskPigstyDAO.flush();

		taskPigstyDAO.remove(related_taskpigsty);
		taskPigstyDAO.flush();

		return feedtaskquality;
	}

	/**
	 * Save an existing FodderType entity
	 * 
	 */
	@Transactional
	public FeedTaskQuality saveFeedTaskQualityFodderType(Integer id, FodderType related_foddertype) {
		FeedTaskQuality feedtaskquality = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(id, -1, -1);
		FodderType existingfodderType = fodderTypeDAO.findFodderTypeByPrimaryKey(related_foddertype.getId());

		// copy into the existing record to preserve existing relationships
		if (existingfodderType != null) {
			existingfodderType.setId(related_foddertype.getId());
			existingfodderType.setName(related_foddertype.getName());
			existingfodderType.setSupply(related_foddertype.getSupply());
			existingfodderType.setMemo(related_foddertype.getMemo());
			related_foddertype = existingfodderType;
		}

		feedtaskquality.setFodderType(related_foddertype);
		related_foddertype.getFeedTaskQualities().add(feedtaskquality);
		feedtaskquality = feedTaskQualityDAO.store(feedtaskquality);
		feedTaskQualityDAO.flush();

		related_foddertype = fodderTypeDAO.store(related_foddertype);
		fodderTypeDAO.flush();

		return feedtaskquality;
	}

	/**
	 * Load an existing FeedTaskQuality entity
	 * 
	 */
	@Transactional
	public Set<FeedTaskQuality> loadFeedTaskQualitys() {
		return feedTaskQualityDAO.findAllFeedTaskQualitys();
	}

	/**
	 * Return a count of all FeedTaskQuality entity
	 * 
	 */
	@Transactional
	public Integer countFeedTaskQualitys() {
		return ((Long) feedTaskQualityDAO.createQuerySingleResult("select count(o) from FeedTaskQuality o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing FeedTaskQuality entity
	 * 
	 */
	@Transactional
	public void saveFeedTaskQuality(FeedTaskQuality feedtaskquality) {
		FeedTaskQuality existingFeedTaskQuality = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(feedtaskquality.getId());

		if (existingFeedTaskQuality != null) {
			if (existingFeedTaskQuality != feedtaskquality) {
				existingFeedTaskQuality.setId(feedtaskquality.getId());
				existingFeedTaskQuality.setMemo(feedtaskquality.getMemo());
			}
			feedtaskquality = feedTaskQualityDAO.store(existingFeedTaskQuality);
		} else {
			feedtaskquality = feedTaskQualityDAO.store(feedtaskquality);
		}
		feedTaskQualityDAO.flush();
	}

	/**
	 * Return all FeedTaskQuality entity
	 * 
	 */
	@Transactional
	public List<FeedTaskQuality> findAllFeedTaskQualitys(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<FeedTaskQuality>(feedTaskQualityDAO.findAllFeedTaskQualitys(startResult, maxRows));
	}

	/**
	 * Delete an existing FodderType entity
	 * 
	 */
	@Transactional
	public FeedTaskQuality deleteFeedTaskQualityFodderType(Integer feedtaskquality_id, Integer related_foddertype_id) {
		FeedTaskQuality feedtaskquality = feedTaskQualityDAO.findFeedTaskQualityByPrimaryKey(feedtaskquality_id, -1, -1);
		FodderType related_foddertype = fodderTypeDAO.findFodderTypeByPrimaryKey(related_foddertype_id, -1, -1);

		feedtaskquality.setFodderType(null);
		related_foddertype.getFeedTaskQualities().remove(feedtaskquality);
		feedtaskquality = feedTaskQualityDAO.store(feedtaskquality);
		feedTaskQualityDAO.flush();

		related_foddertype = fodderTypeDAO.store(related_foddertype);
		fodderTypeDAO.flush();

		fodderTypeDAO.remove(related_foddertype);
		fodderTypeDAO.flush();

		return feedtaskquality;
	}

	/**
	 * Delete an existing FeedTaskQuality entity
	 * 
	 */
	@Transactional
	public void deleteFeedTaskQuality(FeedTaskQuality feedtaskquality) {
		feedTaskQualityDAO.remove(feedtaskquality);
		feedTaskQualityDAO.flush();
	}
}
